package com.dfd.store.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.dfd.store.domain.User;

/**
 * Servlet Filter implementation class AuthorityFilter
 */
public class AuthorityFilter implements Filter {

	public AuthorityFilter() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// 判断session中用户是否登录
		HttpServletRequest myRequest = (HttpServletRequest) request;
		User user = (User) myRequest.getSession().getAttribute("loginUser");
		// 有则放行,无则跳转到登录页面
		if (user == null) {
			myRequest.setAttribute("msg", "请先登录");
			myRequest.getRequestDispatcher("/jsp/info.jsp").forward(request, response);

		}else{
			
			chain.doFilter(request, response);
		}

	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
